package com.whli.jee.system.service.impl;

import com.whli.jee.common.web.dao.IBaseDao;
import com.whli.jee.common.web.entity.BaseTree;
import com.whli.jee.common.web.service.impl.BaseServiceImpl;
import com.whli.jee.system.dao.ISysMenuDao;
import com.whli.jee.system.dao.ISysRoleMenuDao;
import com.whli.jee.system.dao.ISysUserDao;
import com.whli.jee.system.entity.SysMenu;
import com.whli.jee.system.entity.SysUser;
import com.whli.jee.system.service.ISysMenuService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author whli
 * @version 1.0
 * @since 1.0
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements ISysMenuService {

    @Autowired
    private ISysMenuDao sysMenuDao;
    @Autowired
    private ISysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private ISysUserDao sysUserDao;

    @Override
    public IBaseDao<SysMenu> getDao() {
        return sysMenuDao;
    }

    @Override
    public int save(SysMenu entity) {
        int rows = super.save(entity);
        if ("TAB".equals(entity.getTarget())){
            List<SysMenu> buttons = new ArrayList<SysMenu>();
            //新增按钮
            SysMenu addBtn = new SysMenu();
            addBtn.setParentId(entity.getId());
            addBtn.setEnable("1");
            addBtn.setHref("btn_add");
            addBtn.setSort(1);
            addBtn.setName("新增");
            addBtn.setTarget("BUTTON");
            buttons.add(addBtn);
            //修改按钮
            SysMenu updateBtn = new SysMenu();
            updateBtn.setParentId(entity.getId());
            updateBtn.setEnable("1");
            updateBtn.setHref("btn_edit");
            updateBtn.setSort(2);
            updateBtn.setName("编辑");
            updateBtn.setTarget("BUTTON");
            buttons.add(updateBtn);
            //删除按钮
            SysMenu delBtn = new SysMenu();
            delBtn.setParentId(entity.getId());
            delBtn.setEnable("1");
            delBtn.setHref("btn_delete");
            delBtn.setSort(3);
            delBtn.setName("删除");
            delBtn.setTarget("BUTTON");
            buttons.add(delBtn);
            super.saveBatch(buttons);
        }

        return rows;
    }

    /**
     * 查询登录用户的按钮
     * @param userId
     * @param parentUrl
     * @return
     */
    @Override
    public List<SysMenu> listButtonsByUserAndParentAndOffice(String userId, String parentUrl,String officeId) {
        List<SysMenu> menus = null;
        if (StringUtils.isNotBlank(userId)) {
            SysUser user = sysUserDao.getByPK(userId);
            if ("1".equals(user.getIsSuper())){
                menus = sysMenuDao.listButtonsByParentUrl(parentUrl);
            }else{
                menus = sysMenuDao.listButtonsByUserAndParentAndOffice(userId, parentUrl,officeId);
            }
        }
        return menus;
    }

    /**
     * 以树结构展示菜单
     * @param parentId
     * @return
     */
    @Override
    public List<BaseTree> listMenuTrees(String parentId) {
        List<BaseTree> trees = sysMenuDao.listMenuTrees(parentId);
        return trees;
    }

    @Override
    public SysMenu getByParentIdAndSort(SysMenu entity) {
        return sysMenuDao.getByParentIdAndSort(entity);
    }

    @Override
    public List<SysMenu> listByNotButton(String parentId) {
        return sysMenuDao.listByNotButton(parentId);
    }

    @Override
    public List<SysMenu> listByUserAndOfficeAndParent(String userId, String parentId, String officeId) throws Exception{
        List<SysMenu> sysMenus = new ArrayList<>();

        SysUser user = sysUserDao.getByPK(userId);
        if ("1".equals(user.getIsSuper())){ //超级管理员
            sysMenus = sysMenuDao.listByNotButton(parentId);
            addChild(sysMenus);

        }else {
            sysMenus = sysMenuDao.listByUserAndOfficeAndParent(userId,parentId,officeId);
        }
        return sysMenus;
    }

    //递归查询子菜单
    private void addChild(List<SysMenu> menus) throws Exception{
        for (int i = 0, m = menus.size(); i < m; i++) {
            List<SysMenu> children = sysMenuDao.listByNotButton(menus.get(i).getId());
            if (CollectionUtils.isNotEmpty(children)) {
                menus.get(i).setChildren(children);
                addChild(children);
            }
        }
    }
}
